<?php

namespace App\Exports;


use App\Models\ArchiveManageVolume;
use App\Models\Project;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithTitle;

class ArchiveVolume10Export implements FromCollection, WithColumnWidths, WithStyles, WithCustomStartCell, WithTitle
{
    use Exportable;

    protected $totalRows;
    protected $projectId;
    protected $type;
    protected $keyword;
    protected $selectIds;
    protected $sheetName;
    protected $sheetTitle;

    public function __construct($projectId, $type, $keyword, $selectIds)
    {
        $this->projectId  = $projectId;
        $this->type       = $type;
        $this->keyword    = $keyword;
        $this->selectIds  = $selectIds;
        $this->sheetName  = ArchiveManageVolume::getTypeText($this->type);
        $this->sheetTitle = Project::query()->where('id', $projectId)->value('title') . $this->sheetName . '文件目录';
    }

    public function collection()
    {
        $thisQuery = ArchiveManageVolume::query()
            ->where('project_id', $this->projectId)
            ->where('type',  $this->type);
        if ($this->selectIds) {
            $thisQuery->whereIn('id', to_array($this->selectIds));
        }
        if ($this->keyword) {
            $likeKeyword = $this->keyword;
            $thisQuery->where('file_number', 'like', $likeKeyword)->orWhere('file_number', 'like', $likeKeyword);
        }
        $list = $thisQuery->orderBy('serial_no', 'asc')->get();
        if ($list->isEmpty()) return [];
        $list = $list->toArray();
        $r_list = [];
        foreach ($list as $val) {
            $tmp = [];
            $tmp['serial_no']   = $val['serial_no'];
            $tmp['file_name']   = $val['file_name'];
            $tmp['remark']      = $val['remark'];
            $r_list[] = $tmp;
        }
        $this->totalRows = count($r_list) + 2;

        return collect($r_list);
    }

    /**
     * 从哪个单元格开始渲染数据，数据集合如 $list
     * @return string
     */
    public function startCell(): string
    {
        return 'A3';
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 15, 'B' => 60, 'C' => 15
        ];
    }

    /**
     * 样式设置
     * @param Worksheet $sheet
     * @throws PhpSpreadsheetException
     */
    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(30); //设置行高(全部行高)
        $sheet->getStyle('A1:C'.$this->totalRows)->getFont()->setName('宋体'); //宋体
        $sheet->getStyle('A1:C'.$this->totalRows)->getFont()->setSize(12); //大小
        $sheet->getStyle('A1:C'.$this->totalRows)->getAlignment()->setWrapText(true); //自动换行

        $sheet->mergeCells('A1:C1');
        $sheet->setCellValue('A1', $this->sheetTitle);

        /**
         * 属性名设置
         */
        $sheet->setCellValue('A2', '序号');
        $sheet->setCellValue('B2', '文件名称');
        $sheet->setCellValue('C2', '备注');

        /**
         * 顶部案卷目录样式设置
         */
        $sheet->getStyle('A1:C' . $this->totalRows)->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1:C' . $this->totalRows)->getAlignment()->setHorizontal('center');//水平居左

        /**
         * 设置边框
         */
        $styleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' =>  Border::BORDER_THIN //细边框
                ]
            ]
        ];
        $sheet->getStyle('A1:C'.$this->totalRows)->applyFromArray($styleArray);
    }

     /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return $this->sheetName;
    }


}
